package com.easy.stock.other;

import java.io.File;

import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * 每周自动下载淘宝的关键词数据
 * 
 * @author linwei
 */
public class KeywordDownloader {
	
	private static Log LOG = LogFactory.getLog("service");

	private final static Pattern NAME_PATTERN = Pattern.compile("“([^”“]+)”");
	private final static Pattern TIME_PATTERN = Pattern.compile("更新时间：([0-9]{2})月([0-9]{2})日");
	private final static Map<String, String> VALID_NAMES = new HashMap<String, String>();
	static {
		VALID_NAMES.put("TOP20W 词表", "top20W$day.xls");
		VALID_NAMES.put("店铺推广词", "dianpu$day.xlsx");
		VALID_NAMES.put("外投词表", "waitoucb$day.xlsx");
	}
	
	private static String url="http://www.taobao.com/go/act/sale/keyword-dictionary.php";
	private static String savePath; // 下载文件的保存位置
	

	public static void main(String args[]) {
		if(args.length!=1) {
			System.out.println("usage: <save path>");
			return;
		}
		
		savePath=args[0];
		
		LOG.info("keyword download begins, savePath="+savePath);
		String content = HttpUtil.getUrl(url, "GBK");
		if(content==null || content.length()==0) {
			LOG.error("keyword download finish, fail to parse url");
			return;
		}
		
		Document doc = Jsoup.parse(content);
		Elements elems = doc.select("#Firstcoke ul.ks-switchable-nav li");
		if(elems.isEmpty()) {
			LOG.error("keyword download finish, fail to parse content");
			return;
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		for(Element elem : elems) {
			
			// 提取标题和日期
			String title = elem.select("span strong").get(0).text();
			Matcher m1 = NAME_PATTERN.matcher(title);
			if(!m1.find()) {
				continue;
			}
			
			Matcher m2 = TIME_PATTERN.matcher(title);
			if(!m2.find()) {
				continue;
			}
			
			String name = VALID_NAMES.get(m1.group(1));
			if(name==null) {
				continue;
			}
			
			// 提取url
			String url = elem.select("a.pic").get(0).attr("href");
			
			Date now = new Date();
			String time = sdf.format(now)+m2.group(1)+m2.group(2);
			
			// 判断是否已经下载过
			String filename = name.replace("$day", time);
			File downFlag = new File(savePath+"/"+filename+".done");
			if(downFlag.exists()) {
				continue;
			}
			
			try {
				FileUtils.copyURLToFile(new URL(url), new File(savePath+"/"+filename));
				FileUtils.touch(downFlag);
				LOG.info("download "+savePath+"/"+filename+" success!");
			} catch (IOException e) {
				LOG.info("download "+savePath+"/"+filename+" fail!, reason=", e);
			}
			

		}
	}
}
